package com.qqt.csr.route.service.strategy.skillset;

import com.google.common.collect.Lists;
import com.qqt.csr.route.dto.resp.SkillSetDTO;
import com.qqt.csr.route.entity.RouteBizRule;
import com.qqt.csr.route.entity.RouteTask;
import com.qqt.csr.route.service.ConversationStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 技能组会话时长低优先分配策略
 */
@Slf4j
@Component
public class SkillSetConversationTimeLeastDistribution implements ISkillSetDistribution {
    @Autowired
    private ConversationStatService conversationStatService;

    @Override
    public SkillSetDTO distribution(RouteTask routeTask, List<SkillSetDTO> skillSetConfigList) {
        Map<Long, SkillSetDTO> skillSetDTOMap = skillSetConfigList.stream().collect(Collectors.toMap(SkillSetDTO::getSkillSetId, v -> v, (o, n) -> o));
        Long conversationLeastTimeSkillSetId = conversationStatService.getConversationLeastTime(routeTask.getTenantId(), Lists.newArrayList(skillSetDTOMap.keySet()));
        if (conversationLeastTimeSkillSetId == null) {
            return skillSetConfigList.get(0);
        }
        return skillSetDTOMap.get(conversationLeastTimeSkillSetId);
    }
}
